package com.sw.bbs.board.controller;

import com.github.pagehelper.PageInfo;
import com.sw.bbs.common.common.GlobalException;
import com.sw.bbs.board.pojo.Post;
import com.sw.bbs.board.service.PostService;
import com.sw.bbs.board.vo.request.post.InsertPostRequest;
import com.sw.bbs.board.vo.request.post.QueryPostRequest;
import com.sw.bbs.board.vo.request.post.UpdatePostRequest;
import com.sw.bbs.board.vo.response.post.PostDetailResponse;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Desc: 帖子API
 *
 * 1. 增删改查
 * 2.
 * * @Author: wusonghe
 * @Date: 2019/3/4 15:26
 */

@Api(value = "帖子模块", description = "帖子模块", tags = {"Post Module"})
@RestController()
@RequestMapping("/posts")
public class PostController {

    private static final Logger logger = LoggerFactory.getLogger(PostController.class);

    @Autowired
    private PostService postService;


    @GetMapping("/list")
    @ApiOperation(value = "分页查询帖子接口", notes = "分页查询帖子", response = Post.class)
    @ApiResponses({@ApiResponse(code = 200, message = "分页结果结构", response = PageInfo
            .class),
            @ApiResponse(code = 201, message = "列表详情", response = Post.class)})
    public ResponseEntity<?> queryPostList(@ApiParam @ModelAttribute QueryPostRequest queryRequest)
            throws GlobalException {
        try {

            PageInfo listResponse = postService.queryPostList(queryRequest);
            return new ResponseEntity<>(listResponse, HttpStatus.OK);
        } catch (Exception e) {
            logger.error("queryPostList", e);
            throw new GlobalException("/posts/list", e.getMessage());
        }
    }

    @GetMapping("/detail")
    @ApiOperation(value = "查询帖子详情接口", notes = "帖子详情", response = PostDetailResponse.class)
    @ApiResponses({@ApiResponse(code = 201, message = "列表详情", response = PostDetailResponse.class)})
    public ResponseEntity<?> queryboardPostDetail(@RequestParam Integer postId)
            throws GlobalException {
        try {
            PostDetailResponse detailResponse = postService.queryPostDetail(postId);
            return new ResponseEntity<>(detailResponse, HttpStatus.OK);
        } catch (Exception e) {
            logger.error("queryPostDetail", e);
            throw new GlobalException("/posts/detail", e.getMessage());
        }
    }

    @PostMapping("/insert")
    @ApiOperation(value = "插入帖子接口", notes = "插入帖子", response = ResponseEntity.class)
    @ApiResponses({@ApiResponse(code = 201, message = "插入", response = ResponseEntity.class)})
    public ResponseEntity<?> insertPost(@ApiParam @ModelAttribute InsertPostRequest request,
                                        HttpServletRequest httpServletRequest)
            throws GlobalException {
        try {
            Integer result = postService.insertPost(request, httpServletRequest);
            return new ResponseEntity<>(result, HttpStatus.OK);
        } catch (Exception e) {
            logger.error("insertPost", e);
            throw new GlobalException("/posts/insert", e.getMessage());
        }
    }

    @PostMapping("/edit")
    @ApiOperation(value = "更新帖子接口", notes = "更新帖子", response = ResponseEntity.class)
    @ApiResponses({@ApiResponse(code = 201, message = "更新帖子", response = ResponseEntity.class)})
    public ResponseEntity<?> editPost(@ApiParam @ModelAttribute UpdatePostRequest request,
                                        HttpServletRequest httpServletRequest)
            throws GlobalException {
        try {
            Integer result = postService.updatePost(request);
            return new ResponseEntity<>(result, HttpStatus.OK);
        } catch (Exception e) {
            logger.error("editPost", e);
            throw new GlobalException("/posts/edit", e.getMessage());
        }
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除帖子接口", notes = "删除帖子", response = ResponseEntity.class)
    @ApiResponses({@ApiResponse(code = 201, message = "更新帖子", response = ResponseEntity.class)})
    public ResponseEntity<?> deletePost(@ApiParam @ModelAttribute Integer postId,
                                        HttpServletRequest httpServletRequest)
            throws GlobalException {
        try {
            Integer result = postService.deletePost(postId);
            return new ResponseEntity<>(result, HttpStatus.OK);
        } catch (Exception e) {
            logger.error("deletePost", e);
            throw new GlobalException("posts/delete", e.getMessage());
        }
    }
}
